import sys
import pygame
import tests
import objects
import common
import math

from pygame.locals import*

class Main:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((500,500))
        self.fps = pygame.time.Clock()
        self.objects = []
        self.objects.append(objects.AABB((170,150),50,50))
        self.objects[-1].update()
        self.objects.append(objects.AABB((160,180),50,50))
        self.objects[-1].update()
        self.objects.append(objects.Poly((200,200),[(40,40),(40,-40),(-40,40)]))
      
        
    def run(self):
        while(1):
            self.fps.tick(40)
            #print self.fps
            
            for event in pygame.event.get(): 
                if event.type == QUIT:
                    sys.exit(0)
                    
                if event.type == pygame.KEYDOWN:
                    if event.key == K_UP:              #set velocity
                        self.objects[1].yvel -= 2      #set velocity
                    if event.key == K_DOWN:            #set velocity
                        self.objects[1].yvel += 2      #set velocity
                    if event.key == K_RIGHT:           #set velocity
                        self.objects[1].xvel += 2      #set velocity
                    if event.key == K_LEFT:            #set velocity
                        self.objects[1].xvel -= 2      #set velocity
                        
                    if event.key == K_w:               # NUDGE
                        self.objects[1].y -= 1         # NUDGE
                    if event.key == K_a:               # NUDGE
                        self.objects[1].x -= 1         # NUDGE
                    if event.key == K_s:               # NUDGE
                        self.objects[1].y += 1         # NUDGE
                    if event.key == K_d:               # NUDGE
                        self.objects[1].x += 1         # NUDGE  
                        
                if event.type == pygame.KEYUP:
                    if event.key == K_UP:
                        self.objects[1].yvel += 2
                    if event.key == K_DOWN:
                        self.objects[1].yvel -= 2
                    if event.key == K_RIGHT:
                        self.objects[1].xvel -= 2
                    if event.key == K_LEFT:
                        self.objects[1].xvel += 2
                        
            self.screen.fill((0,0,100))
            
            for obj in self.objects:
                obj.update()
                
            tests.AABB_AABB(self.objects[1], self.objects[0])
            
            
            for obj in self.objects:
                obj.build()
                
            tests.TRI_AABB(self.objects[2],self.objects[1])    
            tests.TRI_AABB2(self.objects[2],self.objects[0])
            
            pygame.draw.lines(self.screen,(0,255,0),True,self.objects[0].points)
            pygame.draw.lines(self.screen,(255,0,0),True,self.objects[1].points)    
            pygame.draw.lines(self.screen,(50,50,255),True,self.objects[2].points)
            
            pygame.display.flip()

if __name__ == '__main__':
    user = Main()
    #profile.run("td.run()")
    user.run()